<?
function POSTGET($name,$def="") {
	return (isset($_POST[$name])?$_POST[$name]:(isset($_GET[$name])?$_GET[$name]:$def));
}
function GETPOST($name,$def="") {
	return (isset($_GET[$name])?$_GET[$name]:(isset($_POST[$name])?$_POST[$name]:$def));
}
function POSTGETSESSION($name,$default="") {
    if(!isset($_POST[$name]) && !isset($_GET[$name])){
      return (isset($_SESSION[$name]))?$_SESSION[$name]:$default;
    } else {
	  $tmp=POSTGET($name);
      $_SESSION[$name] = $tmp;
      return $tmp;
    }
}

function from_db($val) {
//	return preg_replace("%&amp;#(\d+);%ms","&#\\1;",htmlspecialchars($val));
	return htmlspecialchars($val);
//	return $val;
}

function into_db($val) {
	return mysql_escape_string(stripslashes($val));
//	return $val;
}

/* application log. */
function applog($str,$title){
  $log = APPLICATION_LOG;
  if(($log)&&(trim($str))){
    if ($fp = fopen ($log,"a")) {
	    fwrite($fp,date("[d.m.Y H:i:s]")."$title: $str\n");
    	fclose($fp);
	}
  }
}

function replace_quot($str){
	return str_replace("'","&#39;",$str);
}

// Output a form textarea field
/*  function draw_textarea_value($name,$value = '')
  {

    if ( isset($GLOBALS[$name]) )
	{
      $value = stripslashes($GLOBALS[$name]);
    }
	elseif ($value!='')
		{
      	$value = $value;
	    }
    return $value;
  }


// Output a form input field
  function draw_input_value($name,$value='')
  {

    if ( (isset($GLOBALS[$name])))
	{
      $value = 'value="'.htmlspecialchars(stripslashes($GLOBALS[$name])).'"';
    } elseif ($value!='')
		{
      	$value = 'value="'.htmlspecialchars($value).'"';
	    }
    return $value;
  }


function replaceURLS($message)
{
  $message = preg_replace("#(http://|https://|ftp://|mailto:|news:)([^\s<>,:;'\"]+)(\s|[,:;'\"]|$)#ms",
                         "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>\\3",
                         $message);
  return $message;
}

*/
//validate email
function emailValid($email)
{
if ( preg_match("/^[A-Za-z][A-Za-z0-9_\-]*\@(((([A-Za-z]+([\-]?[A-Za-z0-9])+|[A-Za-z])[\.])+[A-Za-z]{2,4})|((([01]?[0-9]{1,2})|(2(([0-4][0-9])|(5[0-5]))))[\.]){3}(([01]?[0-9]{1,2})|(2(([0-4][0-9])|(5[0-5])))))$/is",$email,$u) )
	{return true;}
	else {return false;}
}

//Show date in american standart

function timestamp_to_date($timestamp) {
	return date("m/d/Y",$timestamp);
}

function timestamp_to_datetime($timestamp, $format="M d, Y h:ia") {
//  setlocale(LC_TIME, 'uk_RU.utf8');
//  putenv('LC_ALL=uk_RU.utf8');
	return strftime($format, $timestamp);
}

function to_date($date) {
	return timestamp_to_date(strtotime($date));
}

function to_datetime($date,$format="M d, Y h:ia") {
	return timestamp_to_datetime(strtotime($date),$format);
}

//	Send e-mail with text and html content based on html content

	function SendMail($from, $to, $subject, $htmlBody) {
		$textBody = strip_tags($htmlBody);

		$textBody = str_replace("&nbsp;", " ", $textBody);
		$textBody = str_replace("&#150;", "-", $textBody);
		$textBody = str_replace("&#151;", "-", $textBody);
		$textBody = str_replace("&quot;", '"', $textBody);

		if (strpos($to, '<')) {
			$posBegin = strpos($to, '<');
			$posEnd = strpos($to, '>');

			$toName = substr($to, 0, $posBegin);
			$to = substr($to, $posBegin + 1, $posEnd - $posBegin - 1);
		} else {
			$toName = "";
		}

		while (strpos($to, " ")) {
			$to = str_replace(" ", "", $to);
		}

		$addresses = explode(",", $to);

		foreach ($addresses as $address) {
//		echo $toName.$address.'<br>';echo $subject.'<br>';echo $htmlBody.'<br>';
		$boundary=md5(uniqid(time()));

			mail("$toName<$address>", $subject, $mm=iconv("utf8","cp1251",
"
This is a multi-part message in MIME format.

------$boundary
Content-Type: text/plain; charset=windows-1251; format=flowed
Content-Transfer-Encoding: 8bit

$textBody


------$boundary
Content-Type: text/html; charset=windows-1251
Content-Transfer-Encoding: 8bit

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html><head>
<meta http-equiv=\"Content-Language\" content=\"ru\">
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1251\">

<style>
body {
	font-family: arial;
	font-size: 80%;
	line-height: 130%;
}
</style>
</head>
<body text=black bgcolor=white lang=ru>
$htmlBody
</body></html>

------$boundary--
"), "From: $from\nMIME-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"------$boundary\"");
		}
	}

function article_prepare_to_display($content) {
        global $zamena;
	$new_content = str_replace(ARTICLE_URL_REPLACER,ARTICLE_URL_PATTERN,$content);
        $new_content = preg_replace(INCLUDE_PATTERN,INCLUDE_REPLACER,$new_content);

        if(isset($zamena))
	{
           foreach($zamena as $key => $value)
           {
	      $new_content = str_replace($key,$value,$new_content);
           }
	}
        return $new_content;
}
function article_prepare_to_edit($content) 
{
/*
	return preg_replace(
		INCLUDE_PATTERN_EDIT,
		INCLUDE_REPLACER_EDIT,
	str_replace(
		ARTICLE_URL_REPLACER,
		ARTICLE_URL_PATTERN,
		$content));
*/
	return str_replace(
		ARTICLE_URL_REPLACER,
		ARTICLE_URL_PATTERN,
		$content);
}
function article_prepare_to_save($content) {
	$content = str_replace("%7E", "~", $content);
	$ret = mysql_escape_string(preg_replace(INCLUDE_PATTERN_SAVE,INCLUDE_REPLACER_SAVE,str_replace(ARTICLE_URL_PATTERN,ARTICLE_URL_REPLACER,stripslashes($content))));
	return $ret;
}
function url_prepare_to_display($url) {
	return htmlspecialchars(str_replace(ARTICLE_URL_REPLACER,ARTICLE_URL_PATTERN,$url));
}
function url_prepare_to_edit($url) {
	return str_replace(ARTICLE_URL_REPLACER,ARTICLE_URL_PATTERN,$url);
}
function url_prepare_to_save($url) {
	return mysql_escape_string(str_replace(ARTICLE_URL_PATTERN,ARTICLE_URL_REPLACER,stripslashes($url)));
}

function include_file($f) {
	do {
		$t=$f;
		$f=str_replace('..','',$f);
	} while ($t!=$f);

	ob_start();
	include(SKIN_PATH.$f);
	$ret=ob_get_contents();
	ob_end_clean();
	return $ret;
}

function GetMenuHTML($current_lang) {
$ret = "";
$CurrentMenuNumber = 1;
	// CURRENT_LANGUAGE_ID;
    $categories = new CategoryBO();
	$categories->menuNumber = $CurrentMenuNumber;
    $categories_lang = new CategoryLangBO();
    $categories_lang->langID = CURRENT_LANGUAGE_ID;
    $categories->leftJoin($categories_lang);
    $categories->parent = "0";
    $categories->visible = "1";
	OB_START();
    $categories->Load(0,0,array($categories->fields[position] => "0"));
	OB_END_CLEAN();
	// echo $categories->last_query;
    $item_n = 0;
    $has_url = false;

    while($categories->Next())
    {
      $has_url = ($categories->pageID>0 || strlen(trim($categories->caturl))>0) ? true : false;
      $URL="";
      if($has_url)
      {
        $URL=($categories->cattype=="0")?(HTTP_SERVER."/page.php?id=".$categories->pageID):(url_prepare_to_display($categories->caturl));
      }
      $NAME = str_replace("'","/'",from_db($categories_lang->name));
	  $ret .= '<a href="'.$URL.'" onfocus="blur()" class="topmenu"';
	  $ret .= ( (strcmp($categories->pageID, GETPOST("id"))==0 )?' id="current"':'');
	  $ret .= '>'.$NAME.'</a>&nbsp;/&nbsp;';
    }
	$ret = substr($ret,0, strlen($ret)-13);
  return $ret;
}

  function show_usa_date_time($date)
  {
     return date('m.d.Y H:i:s',strtotime($date));
  }

	function show_date ($date)
	{
	    if(($date=="0000:00:00")OR($date=="0000-00-00")){return "-";
        }else{
		$month = substr ($date,5,2);
		$day   = substr ($date,8,2);
		$year  = substr ($date,0,4);
		switch ($month) {
			case 1:
				$month1 = "January";
				break;
			case 2:
				$month1 = "February";
				break;
			case 3:
				$month1 = "March";
				break;
			case 4:
				$month1 = "April";
				break;
			case 5:
				$month1 = "May";
				break;
			case 6:
				$month1 = "June";
				break;
			case 7:
				$month1 = "July";
				break;
			case 8:
				$month1 = "August";
				break;
			case 9:
				$month1 = "September";
				break;
			case 10:
				$month1 = "October";
				break;
			case 11:
				$month1 = "November";
				break;
			case 12:
				$month1 = "December";
				break;
		}
		return ("<nobr>".$month1." ".$day.", ".$year."</nobr>");
        }//else
	}

	//2007-02-19 16:32:40
//must return 16:32:40
function show_time($date)
{
		$time = substr ($date,11,8);
		if ($time != "00:00:00")
			return $time;
		else
			return "";
}

//input 2007-02-19 ...
//return 02.19.2007
function show_date_f1 ($date)
{
		$month = substr ($date,5,2);
		$day   = substr ($date,8,2);
		$year  = substr ($date,0,4);
		return ($month.".".$day.".".$year);
}

//March 10, 2007
function show_date_f2 ($date)
{
     if($date=="0000-00-00") return "";
		$month = substr ($date,5,2);
		$day   = substr ($date,8,2);
		$year  = substr ($date,0,4);
		switch ($month)
    {
			case 1: $month1 = "January";  break;
			case 2:	$month1 = "February"; break;
			case 3: $month1 = "March";    break;
			case 4: $month1 = "April"; 		break;
			case 5: $month1 = "May";			break;
			case 6:	$month1 = "June";			break;
			case 7:	$month1 = "July";			break;
			case 8:	$month1 = "August";		break;
			case 9:	$month1 = "September";break;
			case 10:$month1 = "October";	break;
			case 11:$month1 = "November";	break;
			case 12:$month1 = "December";	break;
		}
		return ($month1." ".$day.", ".$year);
}

function thumbnail($filename,$saveTo,$thumbW,$thumbH) {
	//make thumbnail
	$imageInfo = getimagesize($filename);

	$imW = $imageInfo[0];
	$imH = $imageInfo[1];
	if($imW<=$thumbW && $imH<=$thumbH) {
		copy($filename,$saveTo);
		return;
	}

	$ratio = $imW / $imH;

	$thumbRatio = $thumbW / $thumbH;


	if ($ratio > $thumbRatio)  {
		$width = $thumbW;
		$height = round($thumbW / $ratio);
	} else {
		$height = $thumbH;
		$width = round($thumbH * $ratio);
	}

	define(useGD,true);
	if (useGD) {
		$sourceImage = @imagecreatefromjpeg ($filename);
		$targetImage = @imagecreatetruecolor($width, $height);

		imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $width, $height, $imW, $imH);
		imagejpeg($targetImage,$saveTo,80);
	} else {
		copy($filename,$saveTo);
		$cmd = "mogrify -geometry ".$width."x".$height."! ".$saveTo;
		exec(escapeshellcmd($cmd));
		//this lines need only for our mogrify
		//unlink($saveTo);
		//rename(substr($saveTo,0,strlen($saveTo)-3)."mgk",$saveTo);
	}
}

  function formatMoney($number) 
  { 
    if ($number==0) { // zero
          $money = "0.00"; // output zero
    } else { // value
      if (floor($number) == $number) 
      { // whole number
        $money = $number.".00"; // format        
      } else { // cents
        $money = number_format(round($number, 2), 2, '.', ''); // format
      } // integer or decimal
    } // value
    return $money;
  }
?>